﻿using MediatR;
using Tb.App.API.Applications.Command.Basic;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Basic
{
    /// <summary>
    /// 批量删除溯源商品基本信息-命令
    /// </summary>
    public class AllDelBasicCommandHandler : IRequestHandler<AllDelBasicCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<BasicModel> basicRep;

        public AllDelBasicCommandHandler(IBaseRepository<BasicModel> basicRep)
        {
            this.basicRep = basicRep;
        }
        public async Task<ApiResult<int>> Handle(AllDelBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code=ApiEnum.删除成功;
            result.Message=ApiEnum.删除成功.ToString();

            foreach (var item in request.Ids)
            {
                var entity = await basicRep.GetById(item);
                entity.IsDel = true;
                await basicRep.UpdateAsync(entity);
            }
            return await Task.FromResult(result);
        }
    }
}
